package de.neosky.filenamecopier;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetAdapter;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class GUI {

	private final Frame frame;
	private final Dialog dialog;
	private final Label info1;
	private final Label info2;

	private Point movePoint = null;

	public GUI(String title) {
		frame = new Frame();

		dialog = new Dialog(frame, title);
		dialog.setFont(new Font("Sans", Font.PLAIN, 12));
		dialog.setUndecorated(true);
		dialog.setAlwaysOnTop(true);
		dialog.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				close();
			}
		});

		Panel panel = new Panel();
		dialog.add(panel);

		info1 = new Label("Just names", Label.CENTER);
		info1.setBackground(Color.WHITE);
		panel.add(info1);

		info2 = new Label("Full names", Label.CENTER);
		info2.setBackground(Color.LIGHT_GRAY);
		panel.add(info2);

		final Label move = new Label("M", Label.CENTER);
		move.setBackground(Color.GRAY);
		move.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				synchronized (move) {
					dialog.setCursor(Cursor
							.getPredefinedCursor(Cursor.MOVE_CURSOR));
					movePoint = new Point(e.getLocationOnScreen().x
							- dialog.getLocation().x, e.getLocationOnScreen().y
							- dialog.getLocation().y);
				}
			}

			public void mouseReleased(MouseEvent e) {
				synchronized (move) {
					dialog.setCursor(Cursor
							.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
					movePoint = null;
				}
			}
		});
		move.addMouseMotionListener(new MouseMotionAdapter() {

			private final static int ld = 5;
			private final static int rd = 20;

			public void mouseDragged(MouseEvent e) {
				Dimension scr = Toolkit.getDefaultToolkit().getScreenSize();
				Dimension frm = dialog.getSize();

				synchronized (move) {
					if (movePoint != null) {
						int posX = e.getLocationOnScreen().x - movePoint.x;
						int posY = e.getLocationOnScreen().y - movePoint.y;

						if (posX > ld && posX < rd) {
							posX = 0;
						} else {
							int dx = scr.width - posX - frm.width;
							if (dx > ld && dx < rd) {
								posX += dx;
							}
						}

						if (posY > ld && posY < rd) {
							posY = 0;
						} else {
							int dy = scr.height - posY - frm.height;
							if (dy > ld && dy < rd) {
								posY += dy;
							}
						}

						dialog.setLocation(posX, posY);
					}
				}
			}
		});
		panel.add(move);

		Label close = new Label("X", Label.CENTER);
		close.setBackground(Color.GRAY);
		close.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				close();
			}
		});
		panel.add(close);

		info1.setDropTarget(getDropTarget(info1, new String[] { "Just names  ",
				">> Drop <<", "<< copied >>", "<< empty >>" }, false));
		info2.setDropTarget(getDropTarget(info2, new String[] { "Full names",
				">> Drop <<", "<< copied >>", "<< empty >>" }, true));
	}

	private DropTarget getDropTarget(final Label label, final String[] text,
			final boolean flag) {
		return new DropTarget(dialog, new DropTargetAdapter() {

			public void dragExit(DropTargetEvent dte) {
				label.setText(text[0]);
			}

			public void dragEnter(DropTargetDragEvent dtde) {
				label.setText(text[1]);
			}

			public void drop(DropTargetDropEvent dtde) {
				String[] names = Tools.getFilenames(dtde, true, flag);
				if (names != null) {
					String str = "";
					for (String name : names) {
						str += name + "\n";
					}
					Tools.copyTextToClipboard(str);
					label.setText(text[2]);
				} else {
					label.setText(text[3]);
				}
			}
		});
	}

	public void open() {
		dialog.pack();
		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		dialog.setLocation(new Point(d.width - dialog.getWidth(), d.height
				- dialog.getHeight()));
		dialog.setVisible(true);
	}

	protected void close() {
		dialog.setVisible(false);
		frame.dispose();
	}

}
